package com.appspot.didit.client.workitem;

import java.util.ArrayList;
import com.appspot.didit.client.tag.TagManager;
import com.appspot.didit.client.ui.EnhancedTextBox;
import com.appspot.didit.client.util.Util;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;

public class WorkitemManager {
	
	private final WorkitemServiceAsync workitemService = GWT.create(WorkitemService.class);
	
	protected static WorkitemManager theInstance;
	
	protected ArrayList<Workitem> workitemList;
	
	protected Workitem lastCreatedWorkitem;
	
	public FlexTable table = new FlexTable();

	public ArrayList<Workitem> getWorkitemList() {
		if(workitemList == null) {
			workitemList = new ArrayList<Workitem>();
		}
		return workitemList;
	}

	public void setWorkitemList(ArrayList<Workitem> workitemList) {
		this.workitemList = workitemList;
	}

	private WorkitemManager(){
	}
	
	public static WorkitemManager getInstance() {
		if(theInstance == null) {
			theInstance = new WorkitemManager();
		}
		return theInstance;
	}

	public void addWorkitem(Workitem result) {
		if(workitemList == null) {
			workitemList = new ArrayList<Workitem>();
		}
		workitemList.add(0, result);
		setLastCreatedWorkitem(result);
	}

	public Workitem getLastCreatedWorkitem() {
		// TODO Auto-generated method stub
		return this.lastCreatedWorkitem;
	}

	public void setLastCreatedWorkitem(Workitem lastCreatedWorkitem) {
		this.lastCreatedWorkitem = lastCreatedWorkitem;
	}

	@SuppressWarnings("unchecked")
	public void loadWorkitems(Long user_id) {
		workitemService.loadWorkitems(user_id, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to load workitems", caught);
            }
                    
            public void onSuccess(Object result) {
                GWT.log("Successfully loaded workitems", null);
                workitemList = (ArrayList<Workitem>) result;
                getTable();
            }
		});		
	}

	public FlexTable getTable() {
		table.clear();
		Label l_description= new Label("      Description    ");
		l_description.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		Label l_duration= new Label("     Duration             ");
		l_duration.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		Label l_tags= new Label("        Tags              ");
		l_tags.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		Label l_time= new Label("       Time      ");
		l_time.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		table.setWidget(0, 0, l_tags);
		table.setWidget(0, 1, l_description);
		table.setWidget(0, 2, l_duration);
		table.setWidget(0, 3, l_time);
		if(workitemList != null) {
			for(int i = 0, rowCount = 1; i < workitemList.size(); i++, rowCount++) {
				Workitem wi = workitemList.get(i);
/*				EnhancedTextBox description = new EnhancedTextBox(wi.getDescription(), rowCount);
				description.setStylePrimaryName("tb-noborder");
				EnhancedTextBox duration = new EnhancedTextBox(wi.getDuration() + "", rowCount);
				duration.setStylePrimaryName("tb-noborder");
				final EnhancedTextBox tags = new EnhancedTextBox(wi.getTagString(), rowCount);
				tags.addFocusHandler(new FocusHandler() {
					public void onFocus(FocusEvent event) {
						TagCloud tc = TagManager.getInstance().getTagCloud();
						if(!tc.isShowing()) {
							tc.show(tags);
						}
					}
				});
				tags.setStylePrimaryName("tb-noborder");
				table.setWidget(rowCount, 0, tags);
				table.setWidget(rowCount, 1, description);
				table.setWidget(rowCount, 2, duration);
*/				table.setWidget(rowCount, 3, new Label(Util.getDate((wi.getDate()))));
			}
		}
		return table;
	}
	
/*	public void updateTable() {
		table.insertRow(1);
		table.setWidget(1, 0, new EnhancedTextBox(lastCreatedWorkitem.getDescription()));
		table.setWidget(1, 1, new EnhancedTextBox(lastCreatedWorkitem.getDuration() + ""));
		table.setWidget(1, 2, new EnhancedTextBox(lastCreatedWorkitem.getTagString()));
	}*/

	public void update(int rowCount) {
		// TODO Auto-generated method stub
		Workitem wi = workitemList.get(rowCount-1);
		wi.setDescription(((EnhancedTextBox)table.getWidget(rowCount, 1)).getValue());
		wi.setDuration(Integer.parseInt(((EnhancedTextBox)table.getWidget(rowCount, 2)).getValue()));
		System.out.println(((EnhancedTextBox)table.getWidget(rowCount, 0)).getValue());
		TagManager.getInstance().updateTags(wi.getTagString(), ((EnhancedTextBox)table.getWidget(rowCount, 0)).getValue(), wi);
		wi.setTagString(((EnhancedTextBox)table.getWidget(rowCount, 0)).getValue());
		workitemService.update(wi, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to update workitem", caught);
            }
                    
            public void onSuccess(Object result) {
                GWT.log("Successfully updated workitem", null);
            }
		});
	}
	
	public double getWorkitemDuration(Long id) {
		for(Workitem wi : workitemList) {
			if(wi.getId().equals(id)) {
				return wi.getDuration();
			}
		}
		return 0;
	}
}